package com.viknix.threadpool.manager.http.request;

import com.viknix.threadpool.manager.http.Callback;
import com.viknix.threadpool.manager.http.handler.ResponseHandler;
import com.viknix.threadpool.manager.http.model.RequestHttpEntity;
import com.viknix.threadpool.manager.http.response.DefaultClientHttpResponse;
import com.viknix.threadpool.manager.http.result.HttpRestResult;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.concurrent.FutureCallback;
import org.apache.http.impl.nio.client.CloseableHttpAsyncClient;

import java.io.IOException;
import java.net.URI;

/**
 * @Author: Dongqi
 * @Date: 2021/11/22 10:57
 * @Version 1.0
 * @Description: {@link AsyncHttpClientRequest} implementation that uses apache async http client to execute streaming requests.
 */
public class DefaultAsyncHttpClientRequest implements AsyncHttpClientRequest {

    private final CloseableHttpAsyncClient asyncClient;

    public DefaultAsyncHttpClientRequest(CloseableHttpAsyncClient asyncClient) {
        this.asyncClient = asyncClient;
        if (!this.asyncClient.isRunning()) {
            this.asyncClient.start();
        }
    }

    @Override
    public <T> void execute(URI uri, String httpMethod, RequestHttpEntity requestHttpEntity, final ResponseHandler<T> responseHandler,
                            final Callback<T> callback) throws Exception {
        HttpRequestBase httpRequestBase = DefaultHttpClientRequest.build(uri, httpMethod, requestHttpEntity);
        asyncClient.execute(httpRequestBase, new FutureCallback<HttpResponse>() {
            @Override
            public void completed(HttpResponse result) {
                DefaultClientHttpResponse response = new DefaultClientHttpResponse(result);
                try {
                    HttpRestResult<T> httpRestResult = responseHandler.handle(response);
                    callback.onReceive(httpRestResult);
                } catch (Exception e) {
                    callback.onError(e);
                }
            }

            @Override
            public void failed(Exception ex) {
                callback.onError(ex);
            }

            @Override
            public void cancelled() {
                callback.onCancel();
            }
        });

    }

    @Override
    public void close() throws IOException {
        this.asyncClient.close();
    }
}
